package erfgame.frontend.desktop.view.terrain.iso;

import java.util.ArrayList;
import java.util.List;

import erfgame.core.Rotation;
import erfgame.core.entity.Entity;
import erfgame.core.graphics.renderer.EntityRendererFactory;
import erfgame.core.graphics.renderer.chain.ChainedRenderer;
import erfgame.core.graphics.renderer.chain.RendererChain;
import erfgame.core.world.WorldContext;

public class EntitySelection {
	private ArrayList<RendererChain> selected;
	private EntityRendererFactory rendererFactory;
	private int rendererId;
	private WorldContext worldContext;
	
	public EntitySelection( WorldContext worldContext, EntityRendererFactory rendererFactory, int rendererId ) {
		this.selected = new ArrayList<RendererChain>();
		this.rendererFactory = rendererFactory;
		this.rendererId = rendererId;
		this.worldContext = worldContext;
	}

	public void addRendererChain( RendererChain chain, Rotation viewRotation ) {
		if( !this.selected.contains(chain) ) {
			Entity entity = chain.getHead().getEntity();
			ChainedRenderer renderer;
			renderer = (ChainedRenderer)this.rendererFactory.getRenderer(worldContext.getCurrentWorld(), entity, viewRotation);
			chain.add(renderer);
			this.selected.add( chain );
		}
	}
	
	public void removeRendererChain( RendererChain chain ) {
		if( this.selected.remove(chain) ) {
			chain.remove(this.rendererId);
		}
	}
	
	public List<RendererChain> getSelected() {
		return this.selected;
	}
	
	public void clear() {
		for( int i=this.selected.size(); i>0; ) {
			i--;
			RendererChain rendererChain;
			rendererChain = this.selected.get(i);
			rendererChain.remove(this.rendererId);
		}
		this.selected.clear();
	}
}
